package biolabs.visual.j2d

import java.awt.{Graphics2D, Rectangle}

import biolabs.visual.VisualizationMethod

/** Visualization method that draws the visualization to
  * [[java.awt.Graphics2D]].
  */
object Java2D extends VisualizationMethod[Java2DPainter]("Java2D") {

	/** Returns `true`. */
	override def supportsOnScreen() = true

	/** Creates a window that is able to display `Java2DPainter`s. */
	override def createWindow() = Some(new Java2DWindowSink())

	/** Returns a `Java2DPainter` that calls the given painters in sequence
	  * and whose bounds are the union of the given painters' bounds.
	  */
	override def combine(painters: Iterable[Java2DPainter]) =
		new Java2DPainter {
			lazy val bounds = painters map { _.getBounds() } reduce{ (a, b) => a.add(b); a }

			// Rectangle is mutable, therefore return a copy
			override def getBounds() = new Rectangle(bounds)

			override def paint(g: Graphics2D) = for(p <- painters) p.paint(g)
		}
}
